<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="成本类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costType">
              <j-dict-select-tag
                placeholder="请输入成本类型"
                dictCode="cost_production_cost_type"
                v-model="model.costType"
                @change="changeValue"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-show="this.basicInstallationValue">
            <a-form-model-item label="请选择基装分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costNo">
              <a-radio-group v-model:value="currentSubcategory" button-style="solid">
                <a-radio-button value="1">人工成本</a-radio-button>
                <a-radio-button value="2">辅材成本</a-radio-button>
                <a-radio-button value="3">主材成本</a-radio-button>
                <a-radio-button value="4">其他</a-radio-button>
              </a-radio-group>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costNo">
              <a-input v-model="model.costNo" placeholder="请输入编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costName">
              <a-input v-model="model.costName" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costNum">
              <a-input-number v-model="model.costNum" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costUnit">
              <a-input v-model="model.costUnit" placeholder="请输入单位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="成本单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costPrice">
              <a-input-number v-model="model.costPrice" placeholder="请输入成本单价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectDescription">
              <a-input v-model="model.projectDescription" placeholder="请输入项目说明"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {filterDictTextByCache} from '@comp/dict/JDictSelectUtil'

  export default {
    name: 'GhCostProductionForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        currentCostType:0,
        currentSubcategory:0,
        basicInstallationValue:false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          costType: [{ required: true, message: '成本类型不能为空' }],
          costName: [{ required: true, message: '名称不能为空' }],
          costNo: [{ required: true, message: '编号不能为空' }],
          costNum: [{ required: true, message: '数量不能为空' },{"pattern": /^[0-9]*$/,"message": "只能输入正数"},],
          costUnit: [{ required: true, message: '单位不能为空' }],
          costPrice: [
            { required: true, message: '成本单价不能为空' },
            {"pattern": /^[0-9]*$/,"message": "只能输入正数"},
          ],
        },
        url: {
          add: "/ghCostProduction/add",
          edit: "/ghCostProduction/edit",
          queryById: "/ghCostProduction/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      changeValue(value){
        if (value == 1){
          this.basicInstallationValue = true;
        }else {
          this.basicInstallationValue = false;
        }
      },
      getCostTypeValue(text){
        return filterDictTextByCache('cost_production_cost_type', text);
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if (this.model.subcategory != null && this.model.subcategory != ""){
          this.basicInstallationValue = true;
          this.currentSubcategory = this.model.subcategory;
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if (this.currentSubcategory != 0){
              this.model.subcategory = this.currentSubcategory;
            }else {
              let costTypeName =  this.getCostTypeValue(this.model.costType);
              this.model.costTypeName = costTypeName;
            }

            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>